package com.stageadminserver.adminserver.service.impl;


import cn.dev33.satoken.secure.SaSecureUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stageadminserver.adminserver.mapper.UserMapper;
import com.stageadminserver.adminserver.service.UserService;
import com.stagecommon.common.pojo.RespBean;
import com.stagecommon.common.pojo.User;
import com.stageuploadserver.uploadserver.utils.MinioUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yxl
 * @since 2021-11-09
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    MinioUtil minioUtil;

    @Override
    public RespBean<User> selectUser(User user) {
        return RespBean.operationSuccess("查询成功",200,userMapper.selectById(user));
    }

    @Override
    public RespBean<IPage<User>> getAllUserByPage(User user, Integer currentPage, Integer size) {
        Page<User> page = new Page<>(currentPage, size);
        return RespBean.operationSuccess("查询成功", 200, userMapper.selectUserListByPage(user.getUsername(), page));
    }

    @Override
    public RespBean deleteUserById(User user) {
        if (userMapper.deleteById(user) == 1) {
            return RespBean.operationSuccess("删除成功", 200, 1);
        }
        return RespBean.operationFail("删除失败");
    }

    @Override
    public RespBean<Integer> changeUserById(User user) {
        if (userMapper.updateById(user) == 1){
            return RespBean.operationSuccess("更新用户状态成功", 200, 1);
        }
        return RespBean.operationFail("更新用户状态失败");
    }

    @Override
    public RespBean<Integer> addUser(User user) {
        user.setPassword(SaSecureUtil.md5(user.getPassword()));
        user.setUserImg("/img/userAvatar/defaultUserImg.jpg");
        if (userMapper.insert(user) == 1) {
            return RespBean.operationSuccess("添加用户成功！", 200, 1);
        }
        return RespBean.operationFail("添加用户失败！");
    }


}
